<template>
  <div class="com-contatiner">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      chertInstane: null,
      allData: '',
      currentIndex: 0,
      timeId: '',
      titleFontSize: 0
    }
  },
  created () {
    this.$socket.registerCallBack('stockData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.startInterval()
    this.chertInstane.on('mousemove', () => {
      clearInterval(this.timeId)
    })
    this.chertInstane.on('mouseout', () => {
      this.startInterval()
    })
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
      value: ''
    })
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timeId)
    this.$socket.unRegisterCallBack('trendData')
  },
  methods: {
    initChart () {
      this.chertInstane = this.$echarts.init(this.$refs.stock_ref, this.theme)

      // 初始化echarts的数据
      const initOption = {
        title: {
          text: '▍ 库存销售量',
          left: 20,
          top: 20
        }
      }
      // 展示
      this.chertInstane.setOption(initOption)
    },
    // 获取服务器数据
    getData (res) {
      // const { data: res } = await this.$http.get('stock')
      this.allData = res
      // 调用updataChart方法
      this.updataChart()
    },
    // 处理数据
    updataChart () {
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const showArr = showData.map((item, index) => {
        return {
          type: 'pie',
          radius: [this.titleFontSize * 2, this.titleFontSize * 2 + 8],
          center: centerArr[index],
          data: [
            {
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  }, // 0的颜色值
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  } // 100的颜色值
                ])
              }
            },
            {
              name: item.name + '\n\n' + item.sales,
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }

            }
          ],
          labelLine: {
            show: false
          },
          hoverAnimation: false,
          label: {
            show: true,
            position: 'center',
            color: colorArr[index][0],
            fontSize: this.titleFontSize / 2
          }
        }
      })
      const dataOption = {
        series: showArr
      }
      this.chertInstane.setOption(dataOption)
    },
    screenAdapter () {
      this.titleFontSize = this.$refs.stock_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        }
      }
      this.chertInstane.setOption(adapterOption)
      this.chertInstane.resize()
    },
    startInterval () {
      this.timeId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > (this.allData.length / 5) - 1) {
          this.currentIndex = 0
        }
        this.updataChart()
      }, 3000)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chertInstane.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updataChart() // 更新图表的展示
    }
  }
}
</script>

<style>
</style>
